package _220309;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author ShadowLim
 * @create 2022-03-09-12:15
 */
public class _排它平方数 {
//    public static void main(String[] args) {
//        Scanner scan = new Scanner(System.in);
//        //在此输入您的代码...
//        long num = 0, cnt = 0;
//        // 每个位置数字不同 从123456开始 最大的符合条件的6位数为987654
//        for (long i = 123456; i <= 987654; i++) {
//            // 求出各个位置上的数字
//            long last = i % 10;
//            long fifth = i / 10 % 10;
//            long fourth = i / 100 % 10;
//            long third = i / 1000 % 10;
//            long second = i / 10000 % 10;
//            long first = i / 100000 % 10;
//            // 提前判断
//            if (i == 203879 || last == fifth || last == fourth || last == third ||
//                    last == second || last == first || fifth == fourth || fifth == third
//                    || fifth == second || fifth == first || fourth == third || fourth == second
//                    || fourth == first || third == second || third == first || second == first) {
//                continue;
//            }
//            num = i * i;
//            String numStr = num + "";   // 将平方数转换为字符串 方便使用contains进行判断
//            char[] iList = (i + "").toCharArray();
//            cnt = 0;
//            for (int k = 0; k < iList.length; k++) {
//                if (numStr.contains(iList[k] + "")) {
//                    cnt++;
//                }
//            }
//            if (cnt == 0) {
//                System.out.println(i);
//            }
//        }
//        scan.close();
//    }

    public static void main(String[] args) {
        long ans = 123456;
        for ( ; ans <= 987654; ans++) {
            if (ans == 203879) {
                continue;
            }
            Set<Long> set = new HashSet<>();
            if (check(set, ans) && test(set, ans * ans)) {
                break;
            }
        }
        System.out.println(ans);    // 639172
    }

    private static boolean check(Set<Long> set, long a) {
        while (a != 0) {
            if (set.contains(a % 10)) {
                return false;
            } else {
                set.add(a % 10);
            }
            a /= 10;
        }
        return  true;
    }

    private static boolean test(Set<Long> set, long b) {
        while ((b != 0)) {
            if (set.contains(b % 10)) {
                return false;
            }
            b /= 10;
        }
        return true;
    }
}
